/*
 * Copyright 2014 NAVER Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ideal.ete.collector.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.ideal.ete.collector.application.Application;
import com.ideal.ete.collector.application.CenterInfo;
import com.ideal.ete.collector.service.CenterInfoService;
import com.ideal.ete.collector.service.CommonService;
import com.ideal.ete.collector.view.ApplicationGroup;
import com.ideal.ete.collector.view.ServerTime;

/**
 * @author emeroad
 * @author netspider
 */
@Controller
public class MainController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CommonService commonService;
    
    @Autowired
    private CenterInfoService centerInfoService;

    @RequestMapping(value = "/applications", method = RequestMethod.GET)
    @ResponseBody
    public Object getApplicationGroup() {
        List<Application> applicationList = commonService.selectAllApplicationNames();
        logger.debug("/applications {}", applicationList);
        return new ApplicationGroup(applicationList);
    }
    
    @RequestMapping(value = "/centers", method = RequestMethod.GET)
    @ResponseBody
    public Object getAllCenters() {
    	Set<String> centers = new HashSet<>();
    	List<CenterInfo> allCenterInfos = centerInfoService.getAllCenterInfos();
    	for (CenterInfo centerInfo : allCenterInfos) {
			centers.add(centerInfo.getCenter());
		}
    	logger.debug("/centers {}", centers);
    	return centers;
    }
    
    @RequestMapping(value = "/getAllCenterInfos", method = RequestMethod.GET)
    @ResponseBody
    public Object getAllCenterInfos() {
    	
    	List<CenterInfo> allCenterInfos = centerInfoService.getAllCenterInfos();
    	
    	return allCenterInfos;
    }
    
    @RequestMapping(value = "/getAppByCenter", method = RequestMethod.GET)
    @ResponseBody
    public Object getAppByCenter(@RequestParam("center") String center) {
    	
    	 List<CenterInfo> centerInfosByCenter = centerInfoService.getCenterInfosByCenter(center);
    	 List<Application> applicationList = commonService.selectAllApplicationNames();
    	 Set<Application> newList = new HashSet<>();
			for (Application application : applicationList) {
		    	 for (CenterInfo centerInfo : centerInfosByCenter) {
		    		 if(application.getName().equals(centerInfo.getApplicationType().getApplicationName())){
		    			 newList.add(application);
		    		 }
		    	 }

			}
		
    	logger.debug("/getAppByCenter {}", centerInfosByCenter);
    	return newList;
    }
    @RequestMapping(value = "/getCenterByApplicationName", method = RequestMethod.GET)
    @ResponseBody
    public String getCenterByApplicationName(@RequestParam("applicationName") String applicationName) {
    	
    	List<CenterInfo> centerList = centerInfoService.getAllCenterInfos();
    	String center = null;
    	long timestamp = 0;
    	for (CenterInfo centerInfo : centerList) {
			if(applicationName.equals(centerInfo.getApplicationType().getApplicationName())){
				if(centerInfo.getTimeStamp() > timestamp){
					center = centerInfo.getCenter();
					timestamp = centerInfo.getTimeStamp();
				}
				
			}
		}
    	
    	
    	
    	
    	logger.debug("/getCenterByApplicationName {}", centerList);
    	return center;
    }
    
    @RequestMapping(value = "/getAppByCenterAndType", method = RequestMethod.GET)
    @ResponseBody
    public Object getAppByCenterAndType(@RequestParam("center") String center,
    		@RequestParam("type") String type) {
    	
    	 List<CenterInfo> centerInfosByCenter = centerInfoService.getCenterInfosByCenter(center);
    	 List<Application> applicationList = commonService.selectAllApplicationNames();
    	 Set<Application> newList = new HashSet<>();
			for (Application application : applicationList) {
		    	 for (CenterInfo centerInfo : centerInfosByCenter) {
		    		 if(application.getName().equals(centerInfo.getApplicationType().getApplicationName())){
//		    			 newList.add(application);
		    			 if(type.equals(centerInfo.getApplicationType().getType())){
		    				 newList.add(application);
		    			 }
		    		 }
		    	 }

			}
		
    	logger.debug("/getAppByCenterAndType {}", centerInfosByCenter);
    	return newList;
    }

    @RequestMapping(value = "/serverTime", method = RequestMethod.GET)
    @ResponseBody
    public ServerTime getServerTime() {
        return new ServerTime();
    }
    
}
